/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.provider.custom;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.asiainfo.ability.base.service.TraceService;
import org.asiainfo.ability.base.utils.StringUtils;
import org.asiainfo.ability.base.vo.CalleeTrace;
import org.asiainfo.ability.provider.configuration.AspectConfig;
import org.asiainfo.ability.provider.utils.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.firewall.FirewalledRequest;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Author: visen
 * @Date: 2018/1/31
 * @Description:
 */

public class ApiFilter implements Filter {
    public static final Logger logger = LogManager.getLogger(ApiFilter.class);


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse
            , FilterChain filterChain) {
        CalleeTrace calleeTrace = null;
        if (servletRequest instanceof FirewalledRequest) {
            RequestContext requestContext = RequestContext.getCurrentContext();
            if (requestContext.get(RequestContext.CALLEE_TRACE) == null) {
                FirewalledRequest request = ((FirewalledRequest) servletRequest);
                String traceId = request.getHeader(RequestContext.TRACE_ID);
                if (!StringUtils.isBlank(traceId)) {
                    calleeTrace = new CalleeTrace();
                    calleeTrace.setTraceId(traceId);
                    calleeTrace.setResponse();
                    calleeTrace.setBeginTime(System.currentTimeMillis());
                    calleeTrace.setBizId(request.getHeader(RequestContext.BIZ_ID));
                    requestContext.put(RequestContext.CALLEE_TRACE, calleeTrace);
                    requestContext.put(RequestContext.RESPONSE_KEY, servletResponse);
                }
            }
            requestContext = null;
        }
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } catch (Exception e) {
            ExceptionUtils.logTrace(logger, e);
            ((HttpServletResponse) servletResponse).setStatus(500);
        } finally {

        }
    }

    private boolean checkSession(String traceId) {
        return true;
    }

    @Override
    public void destroy() {
    }
}
